package com.bjsxt.controller.system;

import com.bjsxt.aspectj.annotation.Log;
import com.bjsxt.aspectj.enums.BusinessType;
import com.bjsxt.dto.UserDto;
import com.bjsxt.service.UserService;
import com.bjsxt.utils.ShiroSecurityUtils;
import com.bjsxt.vo.AjaxResult;
import com.bjsxt.vo.DataGridView;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;

/**
 * @author:liao
 * @Description:TODO 用户
 * @date:2020/12/14
 */
@RequestMapping("system/user")
@RestController
public class UserController {
    @Autowired
    private UserService  userService;

    /**
     * 分页查询
     */
    @GetMapping("listUserForPage")
    public AjaxResult listUserForPage(UserDto userDto){
        DataGridView dataGridView = userService.listUserForPage(userDto);
        return AjaxResult.success("查询成功",dataGridView.getData(),dataGridView.getTotal());
    }

    /**
     * 添加用户
     */
    @PostMapping("addUser")
    @Log(title = "添加用户",businessType = BusinessType.INSERT)
    public AjaxResult addUser(@Validated UserDto userDto){
        userDto.setSimpleUser(ShiroSecurityUtils.getCurrentSimpleUser());
        int i = userService.addUser(userDto);
        return AjaxResult.toAjax(i);
    }

    /**
     * 修改用户
     */
    @PutMapping("updateUser")
    @Log(title = "修改用户",businessType = BusinessType.UPDATE)
    public AjaxResult updateUser(@Validated UserDto userDto){
        userDto.setSimpleUser(ShiroSecurityUtils.getCurrentSimpleUser());
        int i = userService.updateUser(userDto);
        return AjaxResult.toAjax(i);
    }

    /**
     * 根据id查询用户信息
     */
    @RequestMapping("getUserById/{userId}")
    public AjaxResult getUserById(@PathVariable @Validated @NotNull(message = "用户id不能为空") Long userId){
        return AjaxResult.success(userService.getOne(userId));
    }


    /**
     * 删除用户信息
     */
    @DeleteMapping("deleteUserByIds/{userIds}")
    @Log(title = "删除用户信息",businessType = BusinessType.DELETE)
    public AjaxResult deleteUserByIds(@PathVariable @Validated @NotEmpty(message = "要删除的id不能为空") Long[] userIds){
        return AjaxResult.toAjax(userService.deleteUserByIds(userIds));
    }

    /**
     * 查询所有可用的用户
     */
    @GetMapping("selectAllUser")
    public AjaxResult selectAllUser(){
        return AjaxResult.success(userService.getAllUsers());
    }

    /**
     * 重置密码
     */
    @PutMapping("/resetPwd/{userIds}")
    public AjaxResult resetPwd(@PathVariable Long[] userIds){
        if (userIds.length>0){
            userService.resetPassword(userIds);
            return AjaxResult.success("重置成功!");
        }
        return AjaxResult.fail("重置失败,没有选择用户!");
    }


}
